package com.example.bookmall.controller.userSystem;


import com.example.bookmall.entity.address.Address;
import com.example.bookmall.entity.Result;
import com.example.bookmall.entity.usercore.User;
import com.example.bookmall.service.AddressService;
import com.example.bookmall.service.UserService;
import com.example.bookmall.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public Result addAddress(@RequestBody Address address) {

        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);

        address.setUserId(user.getUserId());
        // 如果设置为默认地址，先将其他地址设为非默认
        return addressService.addAddress(address);
    }

    @GetMapping("/list")
    public Result listAddress() {
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User user = userService.findById(userId);

        //TODO：游客是不是也可以查询地址 但是没有
        if (user == null) {
            return Result.error("没有地址可以查询");
        }

        List<Address> addressList=new ArrayList<>();
        addressList = addressService.addressList(user.getUserId());

        return Result.success(addressList);

    }

    //修改地址
    @PutMapping("/modify/{addressId}")
    public Result modifyAddress(@PathVariable Integer addressId,@RequestBody Address address) {
        System.out.println("修改地址"+address+addressId);
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);
        //TODO：游客是不是也可以修改地址 但是没有
        if (user == null) {
            return Result.error("没有地址可以修改");
        }
        return addressService.modifyAddress(addressId,user.getUserId(),address);
    }

    //设置默认地址
    @PutMapping("/setDefault/{addressId}")
    public Result setDefaultAddress(@PathVariable Integer addressId) {
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);
        //TODO：游客是不是也可以设置默认地址 但是没有
        if (user == null) {
            return Result.error("没有地址可以设置默认");
        }
        boolean re=addressService.setDefaultAddress(addressId,user.getUserId());
        return re?Result.success():Result.error("设置默认地址失败");
    }
    //设置默认地址为非默认
    @PutMapping("/setUnDefault/{addressId}")
    public Result setUnDefaultAddress(@PathVariable Integer addressId) {
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");

        boolean re=addressService.setUnDefaultAddress(addressId,userId);
        return re?Result.success():Result.error("设置默认地址失败");
    }

    //删除地址
    @DeleteMapping("/delete/{addressId}")
    public Result deleteAddress(@PathVariable Integer addressId) {

        return addressService.removeById(addressId);
    }

}


